package com.ch.doudemo.widget;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;

/**
 * 头部下拉缩放ScrollView
 */
public class TopScaleScrollView extends ScrollView {

    private int topHeight;
    private int topWidth;
    private View topView;
    private float scale;
    private float firstPosition;

    public TopScaleScrollView(Context context) {
        super(context);
    }

    public TopScaleScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TopScaleScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        setOverScrollMode(OVER_SCROLL_NEVER);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (first) {
            ViewGroup viewGroup = (ViewGroup) getChildAt(0);
            topView = viewGroup.getChildAt(0);
            topHeight = topView.getMeasuredHeight();
            topWidth = topView.getMeasuredWidth();

            Log.d("height===", topHeight + "");
            first = false;
        }
    }

    private boolean isScaling = false;

    private boolean first = true;

    private float rate = 0.1f;

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_MOVE:
                if (!isScaling) {
                    if (getScrollY() == 0) {
                        firstPosition = ev.getY();
                    } else {
                        break;
                    }
                }
                float v = (ev.getY() - firstPosition) * rate;
                if (v < 0) {
                    break;
                }
                setZoom(v);
                isScaling = true;
                break;
            case MotionEvent.ACTION_UP:
                if (isScaling) {
                    reset();
                    isScaling = false;
                }
                break;
        }
        return super.onTouchEvent(ev);
    }

    private void setZoom(float v) {
        ViewGroup.LayoutParams layoutParams = topView.getLayoutParams();
        layoutParams.height = (int) v + topHeight;
        topView.setLayoutParams(layoutParams);
    }

    private void reset() {
        ValueAnimator animator = ValueAnimator.ofInt(topView.getMeasuredHeight(), topHeight);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                //获取当前的height值
                int h = (int) valueAnimator.getAnimatedValue();
                //动态更新view的高度
                topView.getLayoutParams().height = h;
                topView.requestLayout();
            }
        });
        animator.setDuration(500);
        animator.start();
    }
}
